#include "stdafx.h"
#include "Goods.h"
#include "base/util/string_number_conversions.h"
#include "CommonUtil.h"
CGoods::CGoods() {
}

CGoods::~CGoods() {
}

FieldValue CGoods::getStrValue(int filed) {

    if (filed == FID_NAME) {
        return { m_name, L"@color/text.def"};
    } else if (filed == FID_NAMECODE) {
        return { m_codeText, L"@color/text.def" };
    }
    FieldValue ft;
    
    auto v = m_values[filed];
    if (v.IsEmpty()) {
        ft.text = L"---";
        ft.color = L"@color/text.def.gray";
        return ft;
    }
    
    ft.text = v; // = L"---";
    
    if (filed == FID_ZHANGDIE || filed == FID_ZDF) {
        if (ft.text.StartsWith(L"-")) {
            ft.color = L"@color/green";
        } else {
            double fValue = 0.f;
            nbase::StringToDouble(CommonUtil::ToUTF8(v.c_str()), &fValue);
            if (v > 0.0000001f) {
                ft.color = L"@color/red";
            } else {
                ft.color = L"@color/def";
            }
        }
    } else if (filed == FID_VOLUME || filed == FID_AMOUNT || filed == FID_PREV_CLOSE) {
        ft.color = L"@text.def";
    } else if(filed == FID_PRICE
              || filed == FID_HIGH
              || filed == FID_LOW
              || filed == FID_OPEN
              || filed == FID_BUY_1
              || filed == FID_SELL_1) {
              
        auto preClose =   m_values[FID_PREV_CLOSE];
        double fPreClose = 0.f;
        
        nbase::StringToDouble(CommonUtil::ToUTF8(preClose.c_str()), &fPreClose);
        
        auto value = m_values[filed];
        double fValue = 0.f;
        
        nbase::StringToDouble(CommonUtil::ToUTF8(value.c_str()), &fValue);
        if (fValue > preClose) {
            ft.color = L"@color/red";
        } else if (fValue < preClose) {
            ft.color = L"@color/green";
        } else {
            ft.color = L"@color/def";
        }
    }
    
    
    
    return ft;
}

SOUI::SStringW CGoods::getName() {
    return m_name;
}

SOUI::SStringW CGoods::getStrCode() {
    return m_codeText;
}

int32_t CGoods::code() {
    return m_code;
}

void CGoods::setValue(int field, const std::string &v) {
    if (field == FID_NAME) {
        m_name = SOUI::SStrCpCvt::FromUTF8(v.c_str());
    } else if (field == FID_NAMECODE) {
        m_codeText = SOUI::SStrCpCvt::FromUTF8(v.c_str());
    } else {
        m_values[field] = SOUI::SStrCpCvt::FromUTF8(v.c_str());
    }
}
